package com.zhiming.common.exception.controller;

import com.zhiming.common.exception.pojo.ExceptionResult;
import com.zhiming.common.exception.pojo.ZmException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 */

@ControllerAdvice
public class ZmExceptionController {


    /**
     * 处理异常方法
     */
    @ExceptionHandler(ZmException.class)  //@ExceptionHandler: 声明需要捕获的异常类型
    @ResponseBody  // 把返回值转换为json格式
    public ResponseEntity<ExceptionResult> handlerException(ZmException e){
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e));
    }
}
